<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/7/2
 * 代驾资费
 * Time: 9:33
 */

namespace App\Http\Controllers\Api\Expenses;


use App\Http\Controllers\Controller;
use App\Http\Requests\DrivingTariffRequest;
use App\Services\DrivingTariffService;
use Illuminate\Http\Request;
class DrivingTariffController extends Controller
{
    /**
     * 分页列表
     * @param Request $request
     * @param TariffRepository $repository
     * @return TariffController|\Illuminate\Http\JsonResponse
     * @Author 姿势就是力量
     */
    public function getPageList(Request $request, DrivingTariffService $service)
    {
        $companyId = (int)$request->get('company_id', '');  //公司id
        $type = (string)$request->get('type', '1');         //分类：1-乘客，2-公司
        $pageSize = (int)$request->get('page_size', 10);
        $name = (string)$request->get('name', '');        //资费名称
        $service_type = (int)$request->get('service_id', '');        //业务类型
        $creator_name = (string)$request->get('creator_name', '');        //创建人
        $modifier_name = (string)$request->get('modifier_name', '');        //修改人
        $date = (string)$request->get('created_at','');                          //日期选择
        $updated_at = (string)$request->get('updated_at','');   //修改时间
        $columns = ['id', 'name', 'created_at','service_type_id', 'company_id', 'created_at', 'updated_at', 'creator_id', 'modifier_id'];
        $list = $service->getPageList($companyId,$service_type,$name,$creator_name,$modifier_name,$type, $date,$updated_at,$pageSize,$columns);
        return $this->sendJson($list);
    }
    /**
     * 资费详情
     * @param Request $request
     * @param TariffRepository $repository
     * @return TariffController|\Illuminate\Http\JsonResponse
     */
    public function getFirst(Request $request, DrivingTariffService $service)
    {
        $id = (int)$request->get('id', '');
        $info = $service->find($id);

        return $this->sendJson($info);
    }

    /**
     * 保存信息
     * @param TariffRequest $request
     * @param TariffRepository $repository
     * @return TariffController|\Illuminate\Http\JsonResponse
     */
    public function save(DrivingTariffRequest $request, DrivingTariffService $service)
    {
        $tariff = $service->saveData($request->post());
        if (!$tariff) return $this->sendError('保存失败');

        return $this->sendSuccess('保存成功');
    }


    /**
     * 删除
     * @param Request $request
     * @param TariffService $service
     * @return TariffController|\Illuminate\Http\JsonResponse
     */
    public function delete(Request $request, DrivingTariffService $service)
    {
        $id = (int)$request->post('id', '');
        $res = $service->delete($id);
        if (!$res) return $this->sendError($service->getError());
        return $this->sendSuccess('删除成功');
    }
}